package translated;

import java.util.*;
import edu.pku.sei.modeler.model.core.*;
import edu.pku.sei.modeler.jet.util.*;
import edu.pku.sei.modeler.model.common.MetaTypedList;

public class InterfaceGen
{
  protected static String nl;
  public static synchronized InterfaceGen create(String lineSeparator)
  {
    nl = lineSeparator;
    InterfaceGen result = new InterfaceGen();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "/**" + NL + " * Copyright (c) Software Institude of PKU" + NL + " * All rights reserved. " + NL + " * This program and the accompanying materials" + NL + " * are made available under the terms of the Eclipse Public License v1.0" + NL + " * which accompanies this distribution, and is available at" + NL + " * http://www.eclipse.org/legal/epl-v10.html" + NL + " *" + NL + " */" + NL + "package ";
  protected final String TEXT_2 = ";" + NL + "// imports:" + NL + "import org.w3c.dom.Element;" + NL + "" + NL + "import edu.pku.sei.gefcommon.descriptor.annotation.MethodProperty;" + NL + "import edu.pku.sei.modeler.model.common.*;" + NL + "import edu.pku.sei.modeler.model.core.*;" + NL + "import edu.pku.sei.modeler.model.xml.XMLUtility;" + NL + "import ";
  protected final String TEXT_3 = ";" + NL;
  protected final String TEXT_4 = NL + "import ";
  protected final String TEXT_5 = ";";
  protected final String TEXT_6 = NL;
  protected final String TEXT_7 = NL + "import ";
  protected final String TEXT_8 = ".";
  protected final String TEXT_9 = ";";
  protected final String TEXT_10 = NL;
  protected final String TEXT_11 = NL + NL + "/**" + NL + " * @generated" + NL + " *" + NL + " */" + NL + "public interface I";
  protected final String TEXT_12 = " ";
  protected final String TEXT_13 = "{" + NL + "\t// getters & setters for attributes" + NL + "\t";
  protected final String TEXT_14 = NL + "\tpublic ";
  protected final String TEXT_15 = " get";
  protected final String TEXT_16 = "();" + NL + "\tpublic void set";
  protected final String TEXT_17 = "(";
  protected final String TEXT_18 = " new";
  protected final String TEXT_19 = ");" + NL + "\t";
  protected final String TEXT_20 = NL + "\t" + NL + "\t// getters for references" + NL + "\t";
  protected final String TEXT_21 = NL + "\tpublic MetaTypedList get";
  protected final String TEXT_22 = "();" + NL + "\t";
  protected final String TEXT_23 = NL + "}" + NL + " " + NL + " " + NL + " " + NL + " ";
  protected final String TEXT_24 = NL + " ";

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
     MetaClass cls = (MetaClass) argument; 
    stringBuffer.append(TEXT_1);
    stringBuffer.append(PackageRetriever.retrievePackage(cls));
    stringBuffer.append(TEXT_2);
    stringBuffer.append(ModelerConstants.pkgBase + "." + ModelerConstants.modelBase + "." + "ModelPackage");
    stringBuffer.append(TEXT_3);
    
	// import dependent classes
	List<String> imports = PackageRetriever.retrieveClassImports(cls);
	for(String tmp : imports) {

    stringBuffer.append(TEXT_4);
    stringBuffer.append(tmp);
    stringBuffer.append(TEXT_5);
    	
	}

    stringBuffer.append(TEXT_6);
    
		// get generalizations
		MetaTypedList generalizationList = cls.getGenerals();
		
		List attributeList = cls.getAttributes();
		List referenceList = cls.getReferences();
		
		List<MetaClass> parentList = new ArrayList<MetaClass>();
		
		StringBuffer parentInterfaceNames = new StringBuffer();
		
		String parentName = "";
		if (generalizationList.size() > 0) {
			for (int i = 0 ; i < generalizationList.size(); i++) {
				MetaGeneralization nonfirstGeneralizationRelationship = (MetaGeneralization)generalizationList.get(i);
				MetaClass parentClass = nonfirstGeneralizationRelationship.getGeneral();
				if(parentClass == null)continue;
				parentList.add(parentClass);
				parentName = "I" + parentClass.getName();
				parentInterfaceNames.append(parentName + ",");			

    stringBuffer.append(TEXT_7);
    stringBuffer.append(PackageRetriever.retrievePackage(parentClass));
    stringBuffer.append(TEXT_8);
    stringBuffer.append(parentName);
    stringBuffer.append(TEXT_9);
    
			}
		}

    stringBuffer.append(TEXT_10);
    
	String parentInterfaces = "";
	int length = parentInterfaceNames.length();
	if ( length > 1) {
	  parentInterfaces = "extends " + parentInterfaceNames.substring(0,length - 1);
	}



    stringBuffer.append(TEXT_11);
    stringBuffer.append(cls.getName());
    stringBuffer.append(TEXT_12);
    stringBuffer.append(parentInterfaces);
    stringBuffer.append(TEXT_13);
    for(int i=0; i<attributeList.size(); i++) {
		MetaAttribute attr = (MetaAttribute)attributeList.get(i);
		String attrName = attr.getName();
		attrName = attrName.substring(0,1).toUpperCase()+attrName.substring(1);
	
    stringBuffer.append(TEXT_14);
    stringBuffer.append(attr.getType());
    stringBuffer.append(TEXT_15);
    stringBuffer.append(attrName);
    stringBuffer.append(TEXT_16);
    stringBuffer.append(attrName);
    stringBuffer.append(TEXT_17);
    stringBuffer.append(attr.getType());
    stringBuffer.append(TEXT_18);
    stringBuffer.append(attrName);
    stringBuffer.append(TEXT_19);
    
	}
	
    stringBuffer.append(TEXT_20);
    for(int i=0; i<referenceList.size(); i++) {
		MetaReference ref = (MetaReference)referenceList.get(i);
		String refName = ref.getName();
		refName = refName.substring(0,1).toUpperCase()+refName.substring(1);
	
    stringBuffer.append(TEXT_21);
    stringBuffer.append(refName);
    stringBuffer.append(TEXT_22);
    
	}
	
    stringBuffer.append(TEXT_23);
    stringBuffer.append(TEXT_24);
    return stringBuffer.toString();
  }
}
